package com.zhkj.txg.module.member.ui

import android.view.View
import androidx.lifecycle.Observer
import com.haibin.calendarview.Calendar
import com.haibin.calendarview.CalendarView
import com.zhkj.lib.base.ViewModelActivity
import com.zhkj.lib.http.HttpResponseStatus
import com.zhkj.lib.utils.SpanUtil
import com.zhkj.txg.R
import com.zhkj.txg.module.member.vm.MemberCenterViewModel
import com.zhkj.txg.utils.CustomMonthView
import kotlinx.android.synthetic.main.activity_member_center.*

class MemberCenterActivity : ViewModelActivity<MemberCenterViewModel>() {
    override fun setViewModel(): MemberCenterViewModel = createViewModel()
    override fun statusTitleBar(): View? = titleCenter
    override val layoutResId: Int = R.layout.activity_member_center

    override fun initData() {
        viewModel.getTask()
    }

    override fun initUI() {
        with(calendarCenter) {
            // 设置日历的视图
            setMonthView(CustomMonthView::class.java)
            // 拦截日历的item点击事件
            setOnCalendarInterceptListener(object : CalendarView.OnCalendarInterceptListener {
                override fun onCalendarInterceptClick(calendar: Calendar?, isClick: Boolean) = Unit
                override fun onCalendarIntercept(calendar: Calendar?): Boolean = true
            })
        }
        // 上一个月
        ivCenterCalendarPrevious.setOnClickListener {
            viewModel.calendar.add(java.util.Calendar.MONTH, -1)
            tvCalenderDate.text = viewModel.getUICalendarStr()
            calendarCenter.scrollToPre()
            viewModel.calendar()
        }
        // 下一个月
        ivCenterCalendarNext.setOnClickListener {
            viewModel.calendar.add(java.util.Calendar.MONTH, 1)
            tvCalenderDate.text = viewModel.getUICalendarStr()
            calendarCenter.scrollToNext()
            viewModel.calendar()
        }
    }

    override fun initViewModel() {
        // 会员中心任务数据观察者
        viewModel.taskLiveData.observe(this, Observer { response ->
            handlerResponseStatus(response.status, response.statusTip)
            if (response.status == HttpResponseStatus.Status.SUCCESS) response.response?.data?.let { data ->
                // 填充数据
                data.task?.let {
                    tvCenterTaskCount.text =
                        String.format(getString(R.string.center_task_order_count_format), it.orderCount)
                    tvCenterTaskAmount.text =
                        String.format(getString(R.string.center_task_order_amount_format), it.orderAmount)
                    tvRewardCount.text = SpanUtil.setMoneySymbol(it.rewardMoney)
                    tvRewardAmount.text = it.couponName
                }
                ivRewardCountStatus.isSelected = data.complete
                ivRewardAmountStatus.isSelected = data.complete
                viewCenterTaskStatus.isSelected = data.complete
                tvCenterTaskStatus.text =
                    getString(if (!data.complete) R.string.center_task_not_complete else R.string.center_task_complete)
                tvCenterTodayCount.text = data.orderCount.toString()
                tvCenterTodayAmount.text = SpanUtil.setMoneySymbol(data.orderAmount.toString())
                tvCalenderDate.text = viewModel.getUICalendarStr()
            }
        })
        // 会员中心日历数据观察者
        viewModel.calendarLiveData.observe(this, Observer { response ->
            handlerResponseStatus(response.status, response.statusTip)
            if (response.status == HttpResponseStatus.Status.SUCCESS) response.response?.data?.days?.forEach {
                // 构造UI参数，并填充
                calendarCenter.addSchemeDate(Calendar().apply {
                    year = viewModel.calendar.get(java.util.Calendar.YEAR)
                    month = viewModel.calendar.get(java.util.Calendar.MONTH) + 1
                    day = it.toInt()
                    scheme = it
                })
            }
        })
    }
}